<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-15 16:45:26
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2023-01-04 22:13:48
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Medical\components\See.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElButton } from 'element-plus'

import { ContentWrap } from '@/components/ContentWrap'
import { Descriptions } from '@/components/Descriptions'
import { UploadMultiple } from '@/components/UploadMultiple'
import { Dialog } from '@/components/Dialog'
import { Table } from '@/components/Table'

// 获取字典
// import { useIcon } from '@/hooks/web/useIcon'
import { useCache } from '@/hooks/web/useCache'
import { zcMedicalAssetDetailedListApi, zcMedicalDrugDetailedListApi } from '@/api/cy/Medical'
const { wsCache } = useCache()

/****************************** 变量 start ******************************/
const dict: any = wsCache.get('dict')
const data = ref<any>({})
// const iconFile = useIcon({ icon: 'ic:outline-file-present' })
// const actionType = ref('')
const dialogVisible = ref(false)
const dialogTitle = ref('')
// const d_row = ref<any>({})
const dFile = ref([])

const d_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '主键',
    type: 'index',
    width: 30
  },
  {
    field: 'asset_code',
    label: '注射编码',
    width: 200
  },
  {
    field: 'asset_code_2',
    label: '项圈编码',
    width: 200
  },
  {
    field: 'earcon_code',
    width: 200,
    label: '耳标编码'
  },
  {
    field: 'breed',
    label: '畜别',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed', cellValue)
    },
    width: 100,
    search: {
      show: true
    }
  },
  {
    field: 'breed_sub',
    label: '品种',
    width: 200,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed_sub', cellValue)
    }
  },
  {
    field: 'purpose',
    label: '用途',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('purpose', cellValue)
    }
  },
  {
    field: 'sex',
    label: '性别',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('animal_sex', cellValue)
    }
  },
  {
    field: 'month_age',
    label: '月龄',
    width: 150
  }
  // {
  //   field: 'action',
  //   width: 60,
  //   fixed: 'right',
  //   label: '操作'
  // }
])
/****************************** 变量 end ******************************/

/****************************** 组件参数 start ******************************/
// 获取参数
const props = defineProps({
  currentRow: {
    type: Object,
    default: () => null
  },
  detailSchema: {
    type: Array,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
/****************************** 组件参数 end ******************************/

/****************************** 函数方法 start ******************************/
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const detailedData = ref()
const DrugdetailedData = ref()

// 调用查看接口获取信息
const getMedicalOrderSee = async () => {
  if (props.id) {
    const res = await zcMedicalAssetDetailedListApi({ medical_order_id: props.id })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      detailedData.value = res.data
    }
    const Yao = await zcMedicalDrugDetailedListApi({ medical_order_id: props.id })
    if (Yao) {
      DrugdetailedData.value = await Yao.data
    }
  } else {
    detailedData.value = []
    DrugdetailedData.value = []
    data.value.fileData = []
  }
}

const y_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  // {
  //   field: 'd_id',
  //   label: 'd_id',
  //   width: 150
  // },
  {
    field: 'drug_name',
    label: '药品名称(内部)',
    width: 170
  },
  {
    field: 'medication_name',
    label: '药品名称(外采)',
    width: 150
  },
  {
    field: 'type',
    label: '类型',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('medical_drug_type', cellValue)
    },
    width: 180
  },
  {
    field: 'amount',
    label: '金额',
    width: 180
  },
  {
    field: 'medication_number',
    label: '数量',
    width: 180
  },
  {
    field: 'note',
    label: '备注',
    width: 150
  }
])
// const action = async (type: string, row: any = {}) => {
//   actionType.value = type
//   switch (type) {
//     case 'file_d':
//       dialogTitle.value = '附件'
//       d_row.value = row
//       if (row.file) {
//         dFile.value = JSON.parse(row.file)
//       } else {
//         dFile.value = []
//       }
//       dialogVisible.value = true
//       break
//     default:
//       console.log('未知类型')
//   }
// }
/****************************** 函数方法 end ******************************/

/****************************** 初始化 start ******************************/
const init = async () => {
  // 调用查看接口查询数据
  await getMedicalOrderSee()
}
init()
/****************************** 初始化 end ******************************/
</script>

<template>
  <div>
    <ContentWrap title="主数据">
      <Descriptions :schema="props.detailSchema" :data="props.currentRow">
        <template #breed="{ row }: { row: any }">
          <div>{{ getDictLabel('breed', row.breed) }}</div>
        </template>
        <template #breed_sub="{ row }: { row: any }">
          <div>{{ getDictLabel('breed_sub', row.breed_sub) }}</div>
        </template>
        <template #state="{ row }: { row: any }">
          <div>{{ getDictLabel('medical_order_state', row.state) }}</div>
        </template>
      </Descriptions>
    </ContentWrap>

    <ContentWrap title="明细数据">
      <Table :columns="d_columns" :data="detailedData" :selection="false" :border="true">
        <template #earcon_breed="{ row }: { row: any }">
          <div>{{ getDictLabel('breed', row.earcon_breed) }}</div>
        </template>
        <template #earcon_breed_sub="{ row }: { row: any }">
          <div>{{ getDictLabel('breed_sub', row.earcon_breed_sub) }}</div>
        </template>
        <template #earcon_purpose="{ row }: { row: any }">
          <div>{{ getDictLabel('purpose', row.earcon_purpose) }}</div>
        </template>
        <template #earcon_sex="{ row }: { row: any }">
          <div>{{ getDictLabel('animal_sex', row.earcon_sex) }}</div>
        </template>
        <template #state="{ row }: { row: any }">
          <div>{{ getDictLabel('medical_order_detailed_state', row.state) }}</div>
        </template>

        <!-- <template #action="{ row }">
          <ElButton circle :icon="iconFile" @click="action('file_d', row)" title="附件" />
        </template> -->
      </Table>
    </ContentWrap>

    <ContentWrap title="药品明细数据">
      <Table :columns="y_columns" :data="DrugdetailedData" :selection="false" :border="true" />
    </ContentWrap>

    <Dialog v-model="dialogVisible" :title="dialogTitle">
      <div>
        <UploadMultiple
          ref="dFileRef"
          v-model="dFile"
          :response="{
            url: 'file_url',
            name: 'file_name',
            file_type: 'file_type'
          }"
          disabled
        />
      </div>

      <template #footer>
        <ElButton @click="dialogVisible = false">关闭</ElButton>
      </template>
    </Dialog>
  </div>
</template>
